#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
import numpy as np

# 开启info日志
tf.logging.set_verbosity(tf.logging.INFO)

# 训练集文件和测试集文件
TRAINING_DATA = "../9783to6/train_8000.csv"
TEST_DATA = "../9783to6/test_1783.csv"


def main(unused_argv):
    # 加载训练集和测试集
    training_set = tf.contrib.learn.datasets.base.load_csv_with_header(
        filename=TRAINING_DATA,
        target_dtype=np.int,
        features_dtype=np.float32)
    test_set = tf.contrib.learn.datasets.base.load_csv_with_header(
        filename=TEST_DATA,
        target_dtype=np.int,
        features_dtype=np.float32)

    # Specify that all features have real-value data
    feature_columns = [tf.contrib.layers.real_valued_column("", dimension=6)]

    # Build 3 layer DNN with 10, 20, 10 units respectively.
    classifier = tf.contrib.learn.DNNClassifier(feature_columns=feature_columns,
                                                hidden_units=[10],
                                                n_classes=6,
                                                optimizer=tf.train.ProximalAdagradOptimizer(
                                                    learning_rate=0.1,
                                                    l1_regularization_strength=0.001
                                                ),
                                                model_dir="my_model")

    # Fit model.
    classifier.fit(x=training_set.data,
                   y=training_set.target,
                   steps=10000)

    # Evaluate accuracy.
    evaluate = classifier.evaluate(x=test_set.data,
                                   y=test_set.target)
    # print(evaluate)
    accuracy_score = evaluate["accuracy"]
    print('Accuracy: {0:f}'.format(accuracy_score))

    # Classify two new flower samples.
    new_samples = np.array(
        [[2211, 9.8, 9.7, 74, 24, 116], [1939, 9.8, 9.3, 106, 24, 107], [1503, 9.5, 10.7, 119, 44, 117]], dtype=float)
    y = list(classifier.predict(new_samples, as_iterable=True))
    print('Predictions: {}'.format(str(y)))


if __name__ == "__main__":
    tf.app.run()
